<template>
  <div class="image-expand-page">
    <div class="page-header">
      <h1 class="page-title">图片扩图</h1>
      <p class="page-description">AI智能扩展图片边界，生成自然的扩展区域，支持多种扩展模式</p>
    </div>

    <div class="main-content">
      <!-- 左侧控制面板 -->
      <div class="control-panel">
        <div class="control-section">
          <h3 class="section-title">上传图片</h3>
          <div class="upload-zone" @click="triggerUpload">
            <input 
              ref="fileInput" 
              type="file" 
              accept="image/*" 
              @change="handleImageUpload"
              style="display: none"
            />
            <div class="upload-content">
              <div class="upload-icon">🖼️</div>
              <p>点击上传图片</p>
              <span class="upload-hint">支持 JPG、PNG 格式</span>
            </div>
          </div>
        </div>

        <div class="control-section">
          <h3 class="section-title">扩展方向</h3>
          <div class="direction-grid">
            <button 
              v-for="direction in directions" 
              :key="direction.id"
              :class="['direction-btn', { active: selectedDirection === direction.id }]"
              @click="selectDirection(direction.id)"
            >
              <span class="direction-icon">{{ direction.icon }}</span>
              <span class="direction-name">{{ direction.name }}</span>
            </button>
          </div>
        </div>

        <div class="control-section">
          <h3 class="section-title">扩展尺寸</h3>
          <div class="size-controls">
            <div class="size-input-group">
              <label>宽度扩展</label>
              <div class="input-with-unit">
                <input 
                  v-model="expandWidth" 
                  type="number" 
                  min="0" 
                  max="500"
                  class="size-input"
                />
                <span class="unit">px</span>
              </div>
            </div>
            <div class="size-input-group">
              <label>高度扩展</label>
              <div class="input-with-unit">
                <input 
                  v-model="expandHeight" 
                  type="number" 
                  min="0" 
                  max="500"
                  class="size-input"
                />
                <span class="unit">px</span>
              </div>
            </div>
          </div>
        </div>

        <div class="control-section">
          <h3 class="section-title">生成设置</h3>
          <div class="settings-group">
            <div class="setting-item">
              <label class="setting-label">创意度</label>
              <input 
                v-model="creativity" 
                type="range" 
                min="0" 
                max="100" 
                class="setting-slider"
              />
              <span class="setting-value">{{ creativity }}%</span>
            </div>
            <div class="setting-item">
              <label class="setting-label">边缘融合</label>
              <input 
                v-model="blending" 
                type="range" 
                min="0" 
                max="100" 
                class="setting-slider"
              />
              <span class="setting-value">{{ blending }}%</span>
            </div>
          </div>
        </div>

        <div class="control-section">
          <div class="action-buttons">
            <button class="action-btn primary" @click="generateExpansion" :disabled="!hasImage || isProcessing">
              {{ isProcessing ? '生成中...' : '开始扩图' }}
            </button>
            <button class="action-btn" @click="resetImage" :disabled="!hasImage">
              重新上传
            </button>
            <button class="action-btn" @click="downloadResult" :disabled="!expandedImage">
              下载结果
            </button>
          </div>
        </div>
      </div>

      <!-- 右侧预览区域 -->
      <div class="preview-area">
        <!-- 原图预览 -->
        <div class="preview-container">
          <h3 class="preview-title">原图预览</h3>
          <div class="image-preview original" ref="originalPreview">
            <div v-if="!hasImage" class="empty-state">
              <div class="empty-icon">📷</div>
              <p>请上传图片开始扩图</p>
            </div>
            <img v-else :src="originalImage" alt="原图" class="preview-image" />
          </div>
        </div>

        <!-- 扩图预览 -->
        <div class="preview-container">
          <h3 class="preview-title">扩图预览</h3>
          <div class="image-preview expanded" ref="expandedPreview">
            <div v-if="!expandedImage" class="empty-state">
              <div class="empty-icon">✨</div>
              <p>{{ isProcessing ? 'AI正在生成扩图...' : '点击"开始扩图"生成结果' }}</p>
            </div>
            <div v-else class="expanded-result">
              <img :src="expandedImage" alt="扩图结果" class="preview-image" />
              <!-- 进度指示器 -->
              <div v-if="isProcessing" class="processing-overlay">
                <div class="progress-ring">
                  <div class="progress-circle"></div>
                </div>
                <p class="progress-text">AI生成中... {{ progress }}%</p>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'ImageExpand',
  data() {
    return {
      hasImage: false,
      originalImage: null,
      expandedImage: null,
      isProcessing: false,
      progress: 0,
      selectedDirection: 'all',
      expandWidth: 100,
      expandHeight: 100,
      creativity: 70,
      blending: 80,
      
      directions: [
        { id: 'all', name: '全方向', icon: '🔄' },
        { id: 'horizontal', name: '水平', icon: '↔️' },
        { id: 'vertical', name: '垂直', icon: '↕️' },
        { id: 'top', name: '向上', icon: '⬆️' },
        { id: 'bottom', name: '向下', icon: '⬇️' },
        { id: 'left', name: '向左', icon: '⬅️' },
        { id: 'right', name: '向右', icon: '➡️' }
      ]
    }
  },
  methods: {
    triggerUpload() {
      this.$refs.fileInput.click();
    },
    
    handleImageUpload(event) {
      const file = event.target.files[0];
      if (file) {
        const reader = new FileReader();
        reader.onload = (e) => {
          this.originalImage = e.target.result;
          this.hasImage = true;
          this.expandedImage = null;
        };
        reader.readAsDataURL(file);
      }
    },
    
    selectDirection(directionId) {
      this.selectedDirection = directionId;
    },
    
    async generateExpansion() {
      if (!this.hasImage) return;
      
      this.isProcessing = true;
      this.progress = 0;
      
      // 模拟AI处理进度
      const progressInterval = setInterval(() => {
        this.progress += Math.random() * 15;
        if (this.progress >= 95) {
          this.progress = 95;
          clearInterval(progressInterval);
        }
      }, 300);
      
      // 模拟AI处理过程
      setTimeout(() => {
        clearInterval(progressInterval);
        this.progress = 100;
        this.expandedImage = this.generateMockExpansion();
        this.isProcessing = false;
      }, 3000);
    },
    
    generateMockExpansion() {
      // 创建模拟的扩图结果
      const canvas = document.createElement('canvas');
      const ctx = canvas.getContext('2d');
      
      // 创建临时图片元素
      const img = new Image();
      img.onload = () => {
        // 计算新的画布尺寸
        const newWidth = img.width + (this.selectedDirection === 'horizontal' || this.selectedDirection === 'all' ? this.expandWidth * 2 : 0);
        const newHeight = img.height + (this.selectedDirection === 'vertical' || this.selectedDirection === 'all' ? this.expandHeight * 2 : 0);
        
        canvas.width = newWidth;
        canvas.height = newHeight;
        
        // 填充背景颜色
        ctx.fillStyle = '#f0f0f0';
        ctx.fillRect(0, 0, newWidth, newHeight);
        
        // 计算原图位置
        const offsetX = this.selectedDirection === 'horizontal' || this.selectedDirection === 'all' ? this.expandWidth : 0;
        const offsetY = this.selectedDirection === 'vertical' || this.selectedDirection === 'all' ? this.expandHeight : 0;
        
        // 绘制原图
        ctx.drawImage(img, offsetX, offsetY);
        
        // 添加渐变效果模拟AI扩图
        this.addGradientEffect(ctx, img, offsetX, offsetY, newWidth, newHeight);
      };
      img.src = this.originalImage;
      
      return canvas.toDataURL();
    },
    
    addGradientEffect(ctx, img, offsetX, offsetY, canvasWidth, canvasHeight) {
      // 添加简单的渐变效果来模拟AI扩图
      const gradient = ctx.createLinearGradient(0, 0, canvasWidth, canvasHeight);
      gradient.addColorStop(0, 'rgba(240, 240, 240, 0.3)');
      gradient.addColorStop(1, 'rgba(200, 200, 200, 0.3)');
      
      ctx.fillStyle = gradient;
      ctx.fillRect(0, 0, canvasWidth, canvasHeight);
    },
    
    resetImage() {
      this.hasImage = false;
      this.originalImage = null;
      this.expandedImage = null;
      this.isProcessing = false;
      this.progress = 0;
      this.$refs.fileInput.value = '';
    },
    
    downloadResult() {
      if (this.expandedImage) {
        const link = document.createElement('a');
        link.download = 'image_expansion_result.png';
        link.href = this.expandedImage;
        link.click();
      }
    }
  }
}
</script>

<style scoped>
.image-expand-page {
  padding: 1.5rem;
  min-height: 100vh;
  background: linear-gradient(135deg, #ffecd2 0%, #fcb69f 100%);
}

.page-header {
  text-align: center;
  margin-bottom: 2rem;
}

.page-title {
  font-size: 2.5rem;
  font-weight: bold;
  color: #2d3748;
  margin-bottom: 1rem;
}

.page-description {
  font-size: 1.1rem;
  color: #718096;
  max-width: 600px;
  margin: 0 auto;
}

.main-content {
  display: flex;
  gap: 2rem;
  max-width: 1400px;
  margin: 0 auto;
}

.control-panel {
  width: 320px;
  background: white;
  border-radius: 16px;
  padding: 1.5rem;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
  height: fit-content;
}

.control-section {
  margin-bottom: 2rem;
}

.section-title {
  font-size: 1.1rem;
  font-weight: 600;
  color: #2d3748;
  margin-bottom: 1rem;
}

.upload-zone {
  border: 2px dashed #f6ad55;
  border-radius: 12px;
  padding: 1.5rem;
  text-align: center;
  cursor: pointer;
  transition: all 0.3s ease;
  background: #fffaf0;
}

.upload-zone:hover {
  border-color: #ed8936;
  background-color: #fff5e6;
}

.upload-icon {
  font-size: 2rem;
  margin-bottom: 0.5rem;
}

.upload-hint {
  font-size: 0.875rem;
  color: #a0aec0;
}

.direction-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 0.5rem;
}

.direction-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 0.75rem;
  border: 1px solid #e2e8f0;
  border-radius: 8px;
  background: white;
  cursor: pointer;
  transition: all 0.3s ease;
  gap: 0.25rem;
}

.direction-btn:hover {
  border-color: #f6ad55;
  background-color: #fffaf0;
}

.direction-btn.active {
  border-color: #f6ad55;
  background-color: #fff5e6;
  color: #c05621;
}

.direction-icon {
  font-size: 1.25rem;
}

.direction-name {
  font-size: 0.875rem;
  font-weight: 500;
}

.size-controls {
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.size-input-group {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.size-input-group label {
  font-size: 0.875rem;
  font-weight: 500;
  color: #4a5568;
}

.input-with-unit {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.size-input {
  flex: 1;
  padding: 0.5rem;
  border: 1px solid #e2e8f0;
  border-radius: 6px;
  font-size: 0.875rem;
}

.unit {
  font-size: 0.875rem;
  color: #718096;
  font-weight: 500;
}

.settings-group {
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.setting-item {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.setting-label {
  font-size: 0.875rem;
  font-weight: 500;
  color: #4a5568;
}

.setting-slider {
  width: 100%;
  height: 6px;
  border-radius: 3px;
  background: #e2e8f0;
  outline: none;
}

.setting-value {
  font-size: 0.875rem;
  font-weight: 600;
  color: #f6ad55;
}

.action-buttons {
  display: grid;
  gap: 0.75rem;
}

.action-btn {
  padding: 0.875rem 1rem;
  border: none;
  border-radius: 8px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s ease;
}

.action-btn.primary {
  background: linear-gradient(135deg, #f6ad55 0%, #ed8936 100%);
  color: white;
}

.action-btn.primary:hover:not(:disabled) {
  transform: translateY(-2px);
  box-shadow: 0 8px 20px rgba(246, 173, 85, 0.4);
}

.action-btn:not(.primary) {
  background: #fffaf0;
  color: #c05621;
  border: 1px solid #f6ad55;
}

.action-btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.preview-area {
  flex: 1;
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 1.5rem;
}

.preview-container {
  background: white;
  border-radius: 16px;
  padding: 1.5rem;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
}

.preview-title {
  font-size: 1.1rem;
  font-weight: 600;
  color: #2d3748;
  margin-bottom: 1rem;
  text-align: center;
}

.image-preview {
  width: 100%;
  height: 400px;
  border-radius: 12px;
  overflow: hidden;
  position: relative;
  background: #f7fafc;
  border: 2px dashed #e2e8f0;
}

.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100%;
  color: #a0aec0;
}

.empty-icon {
  font-size: 3rem;
  margin-bottom: 1rem;
}

.preview-image {
  width: 100%;
  height: 100%;
  object-fit: contain;
}

.expanded-result {
  position: relative;
  width: 100%;
  height: 100%;
}

.processing-overlay {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(255, 255, 255, 0.9);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 1rem;
}

.progress-ring {
  width: 60px;
  height: 60px;
  border: 4px solid #e2e8f0;
  border-top: 4px solid #f6ad55;
  border-radius: 50%;
  animation: spin 1s linear infinite;
}

.progress-text {
  font-weight: 600;
  color: #2d3748;
}

@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

/* 响应式设计 */
@media (max-width: 1024px) {
  .main-content {
    flex-direction: column;
  }
  
  .control-panel {
    width: 100%;
  }
  
  .preview-area {
    grid-template-columns: 1fr;
  }
}

@media (max-width: 768px) {
  .page-title {
    font-size: 2rem;
  }
  
  .direction-grid {
    grid-template-columns: repeat(3, 1fr);
  }
}
</style> 
